\insert 'ApplyMoves.oz'

%%
%% Task 4
%%

declare FewFind SplitTrain
fun {FewFind Xs Ys}
   case Ys of Y|_ then
      local Split Foran Bak Moves Na Nb NewXs NewYs in
	 Foran#Bak = {SplitTrain Xs Y}
	 	 
	 Na = 1 + {Length Bak}
	 Nb = {Length Foran}

	 NewXs = {Append Bak Foran}
	 NewYs = {Drop Ys 1}

	 local X in
	    X|_ = Xs
	    if X == Y then
	       Moves = nil
	    else
	       Moves = trackA(Na)|trackB(Nb)|trackA(~Na)|trackB(~Nb)|nil
	    end
	 end
	 
	 {Append Moves {FewFind NewXs NewYs}}
      end
   else
      nil
   end
end

fun {SplitTrain VognListe Vogn}
   local Pos Hs Ts in
      Pos = {Position VognListe Vogn}
      Hs = {Take VognListe (Pos - 1)}
      Ts = {Drop VognListe Pos}
      Hs#Ts
   end
end

